package team.pra.manager.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.pra.manager.common.ManagerResult;
import team.pra.manager.common.PageHelper;
import team.pra.manager.pojo.Account;
import team.pra.manager.service.AccountService;

import javax.servlet.http.HttpServletRequest;

/**
 * 客户的CURD
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @GetMapping("/list")
    @RequiresPermissions({"select"})
    public PageHelper<Account> accounts(String token, PageHelper<Account> pageHelper,
                                      @RequestParam(name = "search[value]") String searchvalue,
                                      @RequestParam(name = "order[0][column]") Integer orderColunm,
                                      @RequestParam(name = "order[0][dir]") String orderDir,
                                      HttpServletRequest request){
        String columnName = request.getParameter("columns["+orderColunm+"][data]");
        pageHelper.setColumnName(columnName);
        pageHelper.setSearchValue(searchvalue);
        pageHelper.setOrderDir(orderDir);
        accountService.findAccountByCondition(pageHelper);
        return pageHelper;
    }

    @PostMapping("/insert")
    @RequiresPermissions({"insert"})
    public ManagerResult insertAccount(String token, Account account) {
        return accountService.insert(account);
    }

    @GetMapping("/delete")
    @RequiresPermissions({"delete"})
    public ManagerResult deleteAccount(String token, Integer id) {
        return accountService.delete(id);
    }
    @PostMapping("/update")
    @RequiresPermissions({"update"})
    public ManagerResult updateAccount(String token, Account account) {
        return accountService.update(account);
    }



}
